package com.ton.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 跨域解决页面请求CORS
 */
@Configuration
// 跨域过滤器
public class CorsConfig {
 @Bean
 CorsFilter corsFilter () {
   CorsConfiguration configuration = new CorsConfiguration ();
   List<String> cors = new ArrayList<>();
       cors.add("http://localhost:9528/");
       cors.add("http://127.0.0.1:9528/");
       cors.add("http://localhost:3000/");
       cors.add("http://127.0.0.1:3000/");
   configuration.setAllowedOrigins(cors);
   configuration.setAllowedMethods(Collections.singletonList("*"));
   //Collections.singletonList("*")
     List<String> headers = new ArrayList<>();
     headers.add("mac");
     headers.add("appId");
     headers.add("access-token");
     headers.add("Content-Type");
     headers.add("user_token");
   configuration.setAllowedHeaders(headers);
   configuration.setAllowCredentials (true);
   UrlBasedCorsConfigurationSource source = new
   UrlBasedCorsConfigurationSource ();
   source.registerCorsConfiguration ("/**" , configuration);
   return new CorsFilter(source);
 }
}